package com.factory._08proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Qinye
 * @date 2023/8/13 11:01
 */
public class VectorProxy  implements InvocationHandler {
    private Object proxyobj;
    public VectorProxy() {
    }


    public VectorProxy(Object object) {
        this.proxyobj = object;
    }

    public static  Object factory(Object object){
        Class<?> cls = object.getClass();
        ClassLoader classLoader = cls.getClassLoader();
        Class<?> [] interfaces = cls.getInterfaces();
        return Proxy.newProxyInstance(classLoader,interfaces,new VectorProxy(object));
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("before calling "+ method);
        if (args != null) {
            for (Object arg : args) {
                System.out.println(arg + "");
            }
        }

        System.out.println("after calling "+ method);
        return  method.invoke(proxyobj, args);
    }
}
